package scau.zzf.interceptor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import scau.zzf.base.exception.UnLoginException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zzf on 2017/4/19.
 */
public class GlobalInterceptor extends HandlerInterceptorAdapter {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object obj) throws Exception {
        String shiroError = (String) httpServletRequest.getAttribute("shiroError");
        if (shiroError!=null&&shiroError.equals("unauthorized")) {
            throw new UnauthorizedException();
        }

        if (SecurityUtils.getSubject().getPrincipal() == null) {
            throw new UnLoginException();
        }

        return true;
    }
}
